﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.Domain.Storages;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.StoragesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.StoragesCommandHandler
{
    public class DelStoreHandler : IRequestHandler<DelStoreCommand, APIResult<object>>
    {
        private readonly IBaseRepository<BaseStorage> _baseStorage;
        private readonly IBaseRepository<SystemLog> _systemLog;
        private readonly IMapper mapper;

        public DelStoreHandler(IBaseRepository<BaseStorage> baseStorage, IMapper mapper, IBaseRepository<SystemLog> systemLog)
        {
            _baseStorage = baseStorage;
            this.mapper = mapper;
            _systemLog = systemLog;
        }

        public async Task<APIResult<object>> Handle(DelStoreCommand request, CancellationToken cancellationToken)
        {
            APIResult<object>result=new APIResult<object>();
            result.Code=APIEnum.删除成功;
            result.Msg = APIEnum.删除成功.ToString();
            var entity = _baseStorage.GetAll().Result.Where(x => x.Cid == request.Cid).FirstOrDefault();
            var res = _baseStorage.Delete(entity).Result;

            //实例化系统日志表
            SystemLog systemlog = new SystemLog();
            systemlog.LoginName = "赵策";
            systemlog.LoginTime = DateTime.Now;
            systemlog.Dosomething = "删除了一条仓库信息";
            await _systemLog.Add(systemlog);

            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                //return Task.FromResult(result);
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                //return Task.FromResult(result);
            }
            return await Task.FromResult(result);
        }
    }
}
